package com.kk.goGame.utils;
/*
 *@author KK on 2023.02.16 21:45
 */

import lombok.experimental.Accessors;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class CodeUtils {
    private static Map<String, Data> codeMap = new ConcurrentHashMap<>();

    @lombok.Data
    @Accessors(chain = true)
    private static class Data {
        private Integer code;
        private Long ms;

    }

    public static Map<String, Data> setCode(String key, Integer verificationCode) {
        codeMap.put(key,new Data().setCode(verificationCode).setMs(System.currentTimeMillis()));
        return codeMap;
    }

    public static Integer getCode(String key) {
        Data data = codeMap.get(key);
        Long ttl = Long.parseLong((String)ProsUtis.getvalue("verificationCode.ttl")) ;
        if (data.getMs()+ttl<System.currentTimeMillis()){
            CodeUtils.remove(key);
            return null;
        }
        return data.getCode();
    }

    public static void remove(String key) {
        codeMap.remove(key);
    }

    public static Integer createCode() {
        int verificationCode = (int) (Math.random() * 9000 + 1000);
        System.out.println(verificationCode);
        return verificationCode;
    }

    public static void main(String[] args) {
        Object getvalue = ProsUtis.getvalue("verificationCode.ttl");
        System.out.println(Long.parseLong((String) getvalue));
    }
}
